package net.wofly.sale.shoppingcart.web;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.order.domain.Order;
import net.wofly.sale.shoppingcart.entity.CartList;
import net.wofly.sale.shoppingcart.service.IShoppingCartService;
import net.wofly.sale.shoppingcart.validator.ShoppingCartValidator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lign
 * @version 1.0
 * @created 29-十月-2017 11:13:50
 */
@RestController
@RequestMapping(value = "/api/shoppingCart")
@Api(description = "购物车接口")
public class ShoppingCartController {

    @Autowired
	IShoppingCartService iShoppingCartService;

	/**
	 * 添加商品到购物车
	 * <ul>
	 * 	<li>判断购物车里是否有此商品，如果有，则修改购物车此商品数量；如果没有新增此商品及数量</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param userID
	 * @param commodityID
	 * @param num
	 */
	@ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车")
	@ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "shoppingCartType", value = "购物车类型", required = true, dataType = "Integer", paramType = "form"),
			@ApiImplicitParam(name = "commodityID", value = "商品ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "num", value = "商品数量", required = true, dataType = "Integer", paramType = "form")})
	@RequestMapping(value = "/addCommodityToCart", method = {RequestMethod.POST})
	public ReturnBean<String> addCommodityToCart(String userID, Integer shoppingCartType, String commodityID, Integer num){
		ReturnBean<String> returnBean;
		String errorMessage = ShoppingCartValidator.validateAddCommodityToCartParams(userID, shoppingCartType ,commodityID, num);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iShoppingCartService.addCommodityToCart(userID, shoppingCartType ,commodityID, num);
		}
		return returnBean;
	}

	/**
	 * 修改商品数量，进入购物车后的操作
	 * <ul>
	 * 	<li>在购物车里修改购物车商品的数量 </li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param cartListID
	 * @param num
	 */
	@ApiOperation(value = "修改商品数量", notes = "修改商品数量")
	@ApiImplicitParams({@ApiImplicitParam(name = "cartListID", value = "购物车列表ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "num", value = "商品数量", required = true, dataType = "Integer", paramType = "form")})
	@RequestMapping(value = "/changeCommodityNum", method = {RequestMethod.POST})
	public ReturnBean<String> changeCommodityNum(String cartListID, Integer num){
		ReturnBean<String> returnBean;
		String errorMessage = ShoppingCartValidator.validateChangeCommodityNumParams(cartListID, num);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iShoppingCartService.changeCommodityNum(cartListID, num);
		}
		return returnBean;
	}

	/**
	 * 商品从购物车里删除
	 * <ul>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param cartListID
	 */
	@ApiOperation(value = "商品从购物车里删除", notes = "商品从购物车里删除")
	@ApiImplicitParam(name = "cartListID", value = "购物车列表ID", required = true, dataType = "String", paramType = "form")
	@RequestMapping(value = "/delCommodity", method = {RequestMethod.POST})
	public ReturnBean<String> delCommodity(String cartListID){
		ReturnBean<String> returnBean;
		String errorMessage = ShoppingCartValidator.validateDelCommodityParams(cartListID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iShoppingCartService.delCommodity(cartListID);
		}
		return returnBean;
	}

	/**
	 *
	 * @param userID
	 */
	@ApiOperation(value = "获取购物车列表", notes = "获取购物车列表")
	@ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String", paramType = "query")
	})
	@RequestMapping(value = "/getCartList", method = {RequestMethod.GET})
	public ReturnBean<List<CartList>> getCartList(String userID, Integer shoppingCartType){
		ReturnBean<List<CartList>> returnBean;
		String errorMessage = ShoppingCartValidator.validateGetCartListParams(userID ,shoppingCartType);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iShoppingCartService.getCartList(userID ,shoppingCartType);
		}
		return returnBean;
	}

	/**
	 *
	 * @param userID
	 */
	@ApiOperation(value = "提交购物车", notes = "提交购物车")
	@ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "carts", value = "购物车的商品列表", required = true, dataType = "List", paramType = "body")
	})
	@RequestMapping(value = "/commitCart", method = {RequestMethod.POST})
	public ReturnBean<Order> commitCart(@RequestParam String userID, @RequestBody String carts) throws CloneNotSupportedException {
		ReturnBean<Order> returnBean;
		String errorMessage = ShoppingCartValidator.validateCommitCartListParams(userID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			List<CartList> cartLists = JSON.parseArray(carts, CartList.class);
			returnBean = iShoppingCartService.commitCart(userID, cartLists);
		}
		return returnBean;
	}

}
